package Year21_Month11.day1105;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class Soultion5 {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList();
        if(root == null){
            return list;
        }

        Stack<TreeNode> stack = new Stack();
        TreeNode cur = root;
        while(!stack.empty() || cur != null){
            while(cur != null){
                stack.push(cur);
                cur = cur.left;
            }
            cur = stack.peek();
            if(cur.right == null){
                list.add(stack.pop().val);
            }else{
                cur = cur.right;
            }
        }
        return list;
    }
}
